package exp1;
import org.apache.http.HttpRequest;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;

@WebFilter(filterName = "LoadingFilter")
public class LoadingFilter implements  Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request=(HttpServletRequest)req;

        HttpSession httpSession = request.getSession();
        String url;
        HttpServletResponse response= (HttpServletResponse)res;
        if (httpSession.getAttribute("username") == null) {
            url="/welcome.jsp";
            //response.sendRedirect("welcome.jsp");
        }
        else
        {
            url="/display.jsp";
            //response.sendRedirect("display.jsp");
        }
        req.getRequestDispatcher(url).forward(req,res);
        chain.doFilter(req,res);
    }

    @Override
    public void destroy() {

    }
}
